#ifndef MES_H
#define MES_H

#include<QSqlDatabase>
#include<QVariant>
#include<QTreeView>
#include<QTableWidget>
#include<QComboBox>
#include<QtNetwork/QNetworkCookieJar>
#include<QtNetwork/QNetworkCookie>


class NetworkCookie : public QNetworkCookieJar
{
    Q_OBJECT

public:
    NetworkCookie(QObject *parent);
    ~NetworkCookie();

    QList<QNetworkCookie> getCookies();
    void setCookies(const QList<QNetworkCookie>& cookieList);
private:

};


class Mes:public QObject
{
    Q_OBJECT

public:
    static Mes* getInstance()
    {
        if(mesInstance==nullptr){
            mesInstance=new Mes;
        }
        return mesInstance;
    }

    static void release()
    {
        if(mesInstance!=nullptr)
        {
            mesInstance->quit();
//            QString name = QSqlDatabase::database().connectionName();
//            if(QSqlDatabase::contains(name)){
//                QSqlDatabase::removeDatabase(name);
//            }
            delete mesInstance;
            mesInstance=nullptr;
        }
    }

    void quit();
    bool initDatabase();
    bool selectData(const QString sqlStr,QList<QMap<QString,QVariant>> &valuesList);
    bool insertData(const QString sqlStr);
    bool updateData(const QString sqlStr);
    bool deleteData(const QString sqlStr);
    bool openDatabase();

    bool updateImport(const QString tableName,const QString NO,const QString remarks);
    bool updateImportFail(const QString tableName,const QString NO,const QString remarks);
    bool insertOneData(const QString tableName,const QStringList &lcolumn,const QStringList &lvalues);
    bool insertAndUpdateUserForm(const QMap<QString,QVariant>& userMap,const QMap<QString,QVariant>& checkMap,const QMap<QString,QVariant>& recheckMap,const QMap<QString,QVariant>& maintainMap);
    bool addUserForm(const QMap<QString,QVariant>& userMap,const QMap<QString,QVariant>& checkMap,const QMap<QString,QVariant>& recheckMap,const QMap<QString,QVariant>& maintainMap);
    bool updateUserForm(const QMap<QString,QVariant>& userMap,const QMap<QString,QVariant>& checkMap,const QMap<QString,QVariant>& recheckMap,const QMap<QString,QVariant>& maintainMap);
    bool updateRole();
    QMap<QString,QString> getClassMap();

    bool classificationTree(QTreeView *tree);         // 设备分类树
    bool roleTable(QTableWidget *table);

    QList<QString> roleList={"员工","组长","经理","管理员"};   // 权限列表
    QMap<int,QString> roleChangeText;

    QList<QMap<QString,QVariant>> getRoleName(const QString workNumber);
    bool getRole(const QString &workNumber);

    QString workNumber;
    QString name;
    QList<QString> role;

    bool loginApi(QString& msg,QString workNumber,QString password);

    bool openDataFlag=false;

    bool getRoleHash(const QString &workNumber);

    QSqlDatabase db;

    bool workNumberIsExist(const QString &workNumer);

    int getCountCheckTable(const QString& threeModel);

    QMap<QString,QList<QList<QVariant>>> getItemMap(const QString& threeMode);

    bool insertCheckRecord(QMap<QString,QList<QList<QVariant>>>& recordMap,const QString& status,const QString& threeMode);

    bool getCheckRecord(int& noPassCheck,int& noPassReCheck,int& noPassMaintain,
        int& passCheck,int& passReCheck,int& passMaintain,int& sumCheck,int& sumReCheck,int& sumMaintain);

    bool NOworkNumberIsExist(QString NO,QString workNumber,int& useAccount);
    bool accountInsertUpdate(QString NO,QString workNumber,int surplus,int scarpAccount,const QString& textReason,const QString& textVer);
    bool insertUpdateRepairForm(QString NO,QString workNumber,const QString& textReason);
    bool repairVer(QString NO,const QString& remarks,QString modelStr,QString userNumber);

    QStringList getCheckItemList(QString col,QString tableName);

    bool getTableData(QList<QStringList> &tableData,QString threeModel,QString checkNameLine);
    bool getName(QString workNumber,QString &newName);
    bool userChange(QString NO,QString oldWorkNumber,QString newWorkNumber,QString reasonText);
    bool checkChange(QString NO,QString oldWorkNumber,QString newWorkNumber,QString reasonText);

    bool repairIsExist(QString NO,QString workNumber);
    bool checkIsExist(QString NO,QString workNumber,bool &flag);

    bool getUserData(const QMap<QString,QVariant>& userMap,QMap<QString,QVariant> &checkMap,QMap<QString,QVariant> &recheckMap,QMap<QString,QVariant> &maintainMap);

private:
    Mes(){};
    Mes(const Mes&){};
    Mes& operator==(const Mes&){};

    static Mes* mesInstance;

    bool pingIsPass(QString ip);

    bool NOIsExist(QString NO,QString workNumber,int &useAccount);

    bool JsonToListList(const QString &msg, QList<QList<QVariant>> &list);
    bool jsonToListMap(const QString &msg, QMap<QString, QVariant> &temp_map);
    bool listMapToJson(const QList<QMap<QString, QVariant>> &list, QString &msg);
    bool ListListToJson(const QList<QList<QVariant>> &list, QString &msg);

//    bool dateFilter(QDateTime dtmp,int cycle);
    bool dateFilterNoMes(QDateTime dtmp,int cycle,QString threeMode,QString NO,QString checkWorkNumber);
    bool dateIsExist(QString date,QString threeMode);
    bool dateIsExist();
    bool checkSumcheckSum(QDateTime dtmp,int cycle,QString threeMode,QString NO);
    bool getSumCheck(int& sum,QString threeModel);
    bool getFinishCheck(int& finish,int& nopass,QString threeModel);
    bool dateIsExistWorkNumber(QString date,QString threeMode,QString NO);
    bool dateIsExistWorkNumber(QString date,QString threeMode,QString NO,QString checkWorkNumber);
    bool restDateIsExist();
    bool checkSum(QDateTime dtmp,int cycle,QString threeMode,QString NO,QString checkWorkNumber);
    bool checkSum(QDateTime dtmp,int cycle,QString threeMode,QString NO);
    bool checkSum(QDateTime dtmp,int cycle);

private slots:
    void comboxBoxChange(const QString &);
};

#endif // MES_H
